﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections;

namespace LCWMS
{
    public partial class  w_rpt030 : DockContent
    {
        DataSet dscustomer;
        DataSet ds_tran;


        public w_rpt030()
        {
            InitializeComponent();
            dgv_tran.AutoGenerateColumns = false;
        }
       

        private void btn_Query_Click(object sender, EventArgs e)
        {
            string str_sql_where = " where a.inv_org = '" + main.g_org + "'  ";
            string str_sql = "select a.inv_org,a.consignor,a.so_no,a.customer_code,a.wholepiece_num,a.box_num,a.sellpiece_num,a.memo,a.made_time from " + main.g_space + "so_apy_card a";
 
            if (dtp_h_time.Text != "")
            {
                if (main.dbtype == "SQL")
                    str_sql_where += " and CONVERT(varchar(10) , a.made_time, 102 ) between '" + dtp_h_time.Text.Trim() + "' and '" + dtp_h_time_end.Text + "'";
                else
                    str_sql_where += " and to_char(a.made_time,'yyyy.mm.dd') between '" + dtp_h_time.Text.Trim() + "' and '" + dtp_h_time_end.Text + "'";
            }
            //判断是否有货主
            if (cb_consignor.Text.Trim() != "")
            {
                str_sql = str_sql + " and a.consignor = '" + cb_consignor.Text.Trim() + "' ";
            }


            if (tb_bill_no.Text != "")
            {
                str_sql_where += " and a.so_no like '%" + tb_bill_no.Text.Trim() + "%'";
            }

            if (tb_shopcode.Text != "")
            {
                str_sql_where += " and a.customer_code like '%" + tb_shopcode.Text.Trim() + "%'";
            }

            //判断是否有线路
            if (cbx_line.Text.Trim() != "")
            {
                string[] lineArr = cbx_line.Text.Trim().Split('-');
                if (lineArr.Length > 1)
                {
                    str_sql_where +=  " and a.customer_code in ( select distinct storeCd from " + main.g_space + ".mst_line where linename like '" + lineArr[1] + "%')  ";
                }
                else
                {
                    str_sql_where += " and a.customer_code in ( select distinct storeCd from " + main.g_space + ".mst_line where linecode like '%" + lineArr[0] + "%' or linename like '%" + lineArr[0] + "%' )  ";
                }
            }
            str_sql = str_sql + str_sql_where;
            try
            {
                if (main.dbtype == "SQL")
                {
                    ds_tran = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    ds_tran = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                if (ds_tran.Tables[0].Rows.Count > 0)
                    dgv_tran.DataSource = ds_tran.Tables[0];
                else
                    dgv_tran.DataSource = null;
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to Query", main.g_info_dataset) + "!" + ee.ToString());
            }
        }

        private void w_inv002_Load(object sender, EventArgs e)
        {
            
            dscustomer = new DataSet();
            
          
            try
            {
                dscustomer = new DataSet();
                dscustomer = main.g_class.comm_get_consignor();
                cb_consignor.DataSource = dscustomer.Tables[0];
                cb_consignor.ValueMember = "consignor";
                cb_consignor.DisplayMember = "consignor";

                //语言转换
                main.g_class.comm_changeControl(this, main.g_win_dataset);
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to initialize Information of transaction type", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            
            dtp_h_time.CustomFormat = "yyyy.MM.dd";
            dtp_h_time.Format = DateTimePickerFormat.Custom;
            dtp_h_time_end.CustomFormat = "yyyy.MM.dd";
            dtp_h_time_end.Format = DateTimePickerFormat.Custom;
            dtp_h_time.Value = System.Convert.ToDateTime(DateTime.Now.ToString("yyyy.MM.dd").Trim());
            dtp_h_time_end.Value = System.Convert.ToDateTime(DateTime.Now.AddDays(7).ToString("yyyy.MM.dd").Trim());

            //线路
            LoadLine();
           
        }

       

        private void dgv_tran_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void lb_batchno_Click(object sender, EventArgs e)
        {

        }

        private void dgv_tran_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           // i_trans_header = e.ColumnIndex;

        }

        private void dgv_tran_Sorted(object sender, EventArgs e)
        {
            //string str_headertext;
            ////先删除合计行
            //((DataTable)dgv_tran.DataSource).Rows.Remove(((DataTable)dgv_tran.DataSource).Select("pack_code = '合计'")[0]);
            ////然后对Tables[0]重新排序
            //DataTable dt;
            //DataView dv = ((DataTable)dgv_tran.DataSource).DefaultView;
            ////得到点击的列
            //str_headertext = dgv_tran.Columns[i_trans_header].DataPropertyName.ToString().Trim();
            //if (b_trans_sort) //升序
            //{
            //    dv.Sort = str_headertext + " DESC";
            //    b_trans_sort = false;
            //}
            //else
            //{
            //    dv.Sort = str_headertext;
            //    b_trans_sort = true;
            //}
            //dt = dv.ToTable();
            //dgv_tran.DataSource = null;
            //dgv_tran.DataSource = dt;
            ////增加合计行
            //f_add_transf();
        }

        private void cb_consignor_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cb_consignor.Text.Trim();
            tb_cusname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void dgv_tran_DoubleClick(object sender, EventArgs e)
        {
             
        }
        private void btn_excel_Click(object sender, EventArgs e)
        {
            if (dgv_tran.Rows.Count > 0)
            {
                main.g_class.DataGridViewToExcel(dgv_tran);
            }
        }


        //线路
        private void LoadLine()
        {
            try
            {
                string str_sql;
                DataSet ds_line;
                //列出属于该波次的列表
                str_sql = "select distinct linecode,(linecode + '-' + linename) linename  from  " + main.g_space + ".mst_line  order by linename ";
                if (main.dbtype == "SQL")
                    ds_line = main.dbservice.Sql_exec_selectsql(str_sql);
                else
                    ds_line = main.dbservice.oracle_exec_selectsql(str_sql);

                cbx_line.ValueMember = "linecode";
                cbx_line.DisplayMember = "linename";

                cbx_line.DataSource = ds_line.Tables[0];
                if (cbx_line.Items.Count > 1)
                {
                    cbx_line.Text = "";
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show("线路加载失败!" + ee.ToString());
            }
        } 

    }
}